﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Douban.Core.HotKeys;
using System.Windows.Input;

namespace Douban.Core.Settings
{
    public class HotKeySetting
    {
        private static string _seperator = " + ";

        public HotKeySetting()
        {
            //ModifierKeys = new ModifierKeys[] { ModifierKeys.Control, ModifierKeys.Alt };
        }

        [XmlArray(ElementName = "modifiers")]
        public ModifierKeys[] Modifiers { get; set; }

        [XmlElement(ElementName = "key")]
        public Key Key { get; set; }

        public void Parse(string hotkeyString)
        {
            string[] hotkeys = hotkeyString.Split(new string[] { _seperator }, StringSplitOptions.RemoveEmptyEntries);
            Modifiers = new ModifierKeys[hotkeys.Length - 1];
            for (int i = 0; i < hotkeys.Length - 1; i++)
                Modifiers[i] = (ModifierKeys)Enum.Parse(typeof(ModifierKeys), hotkeys[i]);

            Key = (Key)Enum.Parse(typeof(Key), hotkeys.Last());
        }

        public override string ToString()
        {
            string result = "";

            if (Modifiers != null)
            {
                foreach (ModifierKeys key in Modifiers)
                {
                    if (key != ModifierKeys.None) 
                        result += (key.ToString() + _seperator);
                }
            }

            result += Key.ToString();

            return result;
        }
    }
}
